<style lang="scss">
  /* 注意要写在第一行，同时给style标签加入lang="scss"属性 */
  @import "uview-ui/index.scss";
</style>
<script>
  export default {
    onLaunch: function() {
		// 授权登录防抖
		uni.setStorageSync('flag', true)
      let token = uni.getStorageSync('userToken')
      if (token.length != 0) {
        // uni.removeStorageSync("userToken")
        this.$store.commit("setToken", uni.getStorageSync('userToken'))
        this.$api.request.userInfo({}, res => {
          // console.log(res);
          if (res.code == 200) {
            // console.log(res);
            this.$store.commit("login", res.data);
            // this.$store.commit("setToken", res.data.tokenValue)
          }
        })
      }
	  /* 小程序更新 */
	  const updateManager = uni.getUpdateManager();
	  updateManager.onCheckForUpdate(function (res) {
	    // 请求完新版本信息的回调
	    console.log(res.hasUpdate);
		if(res.hasUpdate){
			updateManager.onUpdateReady(function (res) {
			  uni.showModal({
			    title: '更新提示',
			    content: '新版本已经准备好，是否重启应用？',
			    success(res) {
			      if (res.confirm) {
			        // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
			        updateManager.applyUpdate();
			      }
			    }
			  });
			
			});
		}
	  });
	  
	  /* 授权定位 */
	  uni.getSetting({
	  	success: res => {
	  		console.log(res.authSetting['scope.userLocation']);
	  		if(!res.authSetting['scope.userLocation']){
	  			uni.getLocation({
	  				type: 'gcj02',
	  				success: function(respp) {
	  					console.log("当前位置的经度：" + respp.longitude);
	  					console.log("当前位置的纬度：" + respp.latitude);
	  					uni.setStorageSync('location', respp.latitude + "," + respp.longitude)
	  				}
	  			})
	  		}
	  	}
	  })
      uni.login({
        success: (res) => {
          // console.log(res);
          this.$api.request.getOpenid({
            code: res.code
          }, res => {
            // console.log(res);
            if (res.code == 200) {
              let data = JSON.parse(res.data)
			  uni.setStorageSync("openId", data.openid)
              this.$store.commit("setOpenId", data)
              // console.log(data.openid);
            }
          })
        }
      })


    },
    onShow: function() {
      console.log('App Show')
    },
    onHide: function() {
      console.log('App Hide')
    },
    create() {
      let token = uni.getStorageSync('userToken')
      if (token.length != 0) {
        this.$store.commit("setToken", uni.getStorageSync('userToken'))
        this.$api.request.userInfo({
          type: 'application/x-www-form-urlencoded',
          'yyt-shop-token': token,
        }, res => {
          // console.log(res);
          if (res.code == 200) {
            this.$store.commit("login", res.data);
            this.$store.commit("setToken", token);
			this.$api.request.getShopCart({}, res => {
				// this.cartList=res.data
				uni.setTabBarBadge({
					index: 2,
					text: res.data.length.toString()
				})
			})
          }
        })
      }
    }
  }
</script>

<style lang='scss'>
  @import './static/css/style.css';
  @import './static/css/iconfont.css';
  /* 图片载入替代方案 */
  /* .app-container {

    color: #EAEAE3;
    height: 100%;
    min-height: 100vh;
    background-color: #F5F5F5 !important;
    background-size: 100% auto !important;
    background: url("@/static/image/topbg.jpg") no-repeat;
  } */

  .image-wrapper {
    font-size: 0;
    background: #f3f3f3;
    border-radius: 4px;

    image {
      width: 100%;
      height: 100%;
      transition: .6s;
      opacity: 0;

      &.loaded {
        opacity: 1;
      }
    }
  }

  /* #ifdef H5 */
  uni-page-head {
    display: none;
  }

  /* #endif */
  .none-hover{
	  opacity: 1;
  }
</style>
